package org.example.demo7.web;

/**
 * @author zyp
 * @version 1.0
 */
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet;

import java.io.IOException;
import java.util.Random;
@WebServlet("/GenerateCaptchaServlet")
public class GenerateCaptchaServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Random random = new Random();
        StringBuilder captchaBuilder = new StringBuilder();

        for (int i = 0; i < 4; i++) {
            int digit = random.nextInt(10);
            captchaBuilder.append(digit);
        }

        String captcha = captchaBuilder.toString();
        request.getSession().setAttribute("captcha", captcha);

        // 返回空响应，因为实际的图片加载将在 JSP 中处理
        response.setContentType("text/plain");
        response.getWriter().write(captcha);
    }
}